package logic

import (
	"context"
	"zk/product_zy17/productRpc/productRpc"

	"zk/product_zy17/api/internal/svc"
	"zk/product_zy17/api/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
)

type AddProductLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewAddProductLogic(ctx context.Context, svcCtx *svc.ServiceContext) *AddProductLogic {
	return &AddProductLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *AddProductLogic) AddProduct(req *types.AddProductRequest) (resp *types.Response, err error) {
	// todo: add your logic here and delete this line

	add, err := l.svcCtx.ProRpc.AddProduct(l.ctx, &productRpc.AddProductRequest{
		Image:  req.Image,
		Title:  req.Title,
		Info:   req.Info,
		Price:  float32(req.Price),
		UserId: req.UserId,
	})
	if err != nil {
		return &types.Response{
			Message: err.Error(),
			Code:    500,
			Data:    nil,
		}, nil
	}

	return &types.Response{
		Message: "商品添加成功",
		Code:    200,
		Data:    add,
	}, nil
}
